package com.smartkey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.project.smartkey.R;
import com.smartkey.sharedpreferences.SharePreferencesUtil;
import com.smartkey.utils.AppUtil;
import com.smartkey.utils.ClickType;

public class MainActivity extends BaseActivity implements OnClickListener {

	private String TAG = "MainActivity";

	private ImageView click, dclick, tclick, img_settings; // 目标app图标
	private TextView txtClick, txtDClick, txtTClick; // 目标app标题
	private RelativeLayout itemClick, itemDClick, itemTClick; // 单击，双击，三击
	private ImageView settings;

	private SharePreferencesUtil preferencesUtil = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sk_activity_main);
		initParam();
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		resumeData();
	}

	private void initParam() {
		preferencesUtil = new SharePreferencesUtil(this);
	}

	/***
	 * 初始化视图
	 */
	private void initView() {

		click = (ImageView) findViewById(R.id.img_icon_click);
		dclick = (ImageView) findViewById(R.id.img_icon_dclick);
		tclick = (ImageView) findViewById(R.id.img_icon_tclick);
		img_settings = (ImageView) findViewById(R.id.img_settings);

		txtClick = (TextView) findViewById(R.id.txt_title_click);
		txtDClick = (TextView) findViewById(R.id.txt_title_dclick);
		txtTClick = (TextView) findViewById(R.id.txt_title_tclick);

		itemClick = (RelativeLayout) findViewById(R.id.item_click);
		itemDClick = (RelativeLayout) findViewById(R.id.item_dclick);
		itemTClick = (RelativeLayout) findViewById(R.id.item_tclick);

		itemClick.setOnClickListener(this);
		itemDClick.setOnClickListener(this);
		itemTClick.setOnClickListener(this);

		settings = (ImageView) findViewById(R.id.img_settings);
		settings.setOnClickListener(this);
		img_settings.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						SettingActivity.class);
				startActivity(intent);
			}
		});
	}

	/**
	 * 恢复数据
	 */
	private void resumeData() {
		AppUtil.resumeSelectedApp(this, preferencesUtil, click, dclick, tclick,
				txtClick, txtDClick, txtTClick);
		Log.d(TAG, String.format("单击app:%s|双击app:%s|三连击:%s",
				txtClick.getText(), txtDClick.getText(), txtTClick.getText()));
	}

	@Override
	public void onClick(View v) {
		ClickType clickType = null;
		switch (v.getId()) {
		case R.id.item_click:
			clickType = ClickType.CLICK;
			break;
		case R.id.item_dclick:
			clickType = ClickType.DCLICK;
			break;
		case R.id.item_tclick:
			clickType = clickType.TCLICK;
			break;
		case R.id.img_settings:
			launchApp();
			return;
		}
		Intent intent = new Intent(this, AppPickWithDefaultActivty.class);
		intent.putExtra("clickType", clickType.getValue());
		startActivity(intent);
	}

	private void launchApp() {
		SharePreferencesUtil preferencesUtil = new SharePreferencesUtil(this);
		boolean isDefault = preferencesUtil.isDefaultSingle();
		String packageName = preferencesUtil.getSingleClickValue();
		AppUtil.launchAppByPackageName(this, packageName, isDefault);
	}

}
